//*-************************* 获取链接信息，渲染到页面
function readget() {
    $.ajax({
        url: "admin/links",
        success: function(res) {
            if (res.status === 0) {
                let arr = []
                res.data.forEach(item => {
                    arr.push(
                        `<tr>
                        <td>${item.id}</td>
                        <td>
                        <div class="bg">
                       <img src="http://localhost:8888/uploads/${item.linkicon}"/>
                        </div>
                        </td>
                        <td>${item.linkname}</td>
                        <td>${item.linkurl}</td>
                        <td>${item.linkdesc}</td>
                        <td><button type="button" data-id='${item.id}' class="layui-btn layui-btn-xs" id='editor'>编辑</button>
                          <button type="button" data-id='${item.id}' class="layui-btn layui-btn-xs  layui-btn-danger">删除</button></td>
                      </tr>`
                    )
                })
                $('.layui-table tbody').html(arr.join(''))
            }
        }
    })
}
readget()




//*-************************* 编辑功能    获取信息*********************
let file
let editoropen
let form = layui.form
let btn = {}

$('tbody').on('click', 'button:contains("编辑")', function() {
    // let data = $(this).data()
    let id = $(this).data('id')
    btn.id = id
    $.ajax({
        type: 'get',
        url: 'admin/links/' + id,
        success: function(res) {
            editoropen = layer.open({
                type: 1,
                title: '编辑链接',
                content: $('#layui-editor-open').html(),
                area: ['500px', '350px'],
            });
            // 设置预览图片效果
            $('#preIcon').attr('src', 'http://localhost:8888/uploads/' + res.data.linkicon)
                // 初始化表单数据
            delete res.data.linkicon
            form.val('editor-form', res.data)


            $('#linkFile').change(function(e) {
                const objectURL = URL.createObjectURL(e.target.files[0])
                file = e.target.files[0]
                console.log(555);
                $('#preIcon').attr('src', objectURL)
            })
        }
    })
})

//

// **************************编辑上传************************

$('body').on('submit', '#editor-form', function(e) {
        // console.log(222222);
        e.preventDefault()
        let fd = new FormData(this)
        if (file) {
            fd.set('linkicon', file)
        }
        $.ajax({
            type: 'put',
            url: 'admin/links/' + btn.id,
            data: fd,
            processData: false,
            contentType: false,
            success: function(res) {
                if (res.status === 0) {
                    layer.msg(res.message)
                    layer.close(editoropen)
                    readget()
                }
            }
        })
    })
    // **************************图片上传************************



$('body').on('click', '#text2', function() {
    $('#linkFile').trigger('click')
})




//*-************************* 删除功能*********************
$('tbody').on('click', 'button:contains("删除")', function() {
    let id = $(this).data('id')
    layer.confirm('确定要删除吗？', function(index) {

        $.ajax({
            type: 'DELETE',
            url: 'admin/links/' + id,
            success: function(res) {
                if (res.status === 0) {
                    layer.msg(res.message)
                    readget()
                }
            }
        })
        layer.close(index);
    })
})


//*-************************* 添加功能*********************
let layer = layui.layer
let indexopen

$('#link').on('click', function() {
    indexopen = layer.open({
        type: 1,
        title: '添加链接',
        content: $('#layui-open').html(),
        area: ['500px', '350px'],
    });

})


// **************************图片上传************************
$('body').on('click', '#test1', function() {
    // console.log(222);
    $('#linkFile').trigger('click')
})
$('#linkFile').change(function(e) {
    const objectURL = URL.createObjectURL(e.target.files[0])
    $('#preIcon').attr('src', objectURL)
})

// *-************************* 提交*********************
$('body').on('submit', '#add-form', function(e) {
    e.preventDefault()
    let fd = new FormData(this)
    $.ajax({
        type: 'post',
        url: 'admin/links',
        data: fd,
        processData: false,
        contentType: false,
        success: function(res) {
            if (res.status === 0) {
                layer.msg(res.message)
                readget()
                layer.close(indexopen)
            }
        }
    })
})